package org.swidgets.base.forms;

import java.util.ArrayList;
import java.util.List;

import org.swidgets.base.Validate;
import org.swidgets.base.spanels.SFormPanel;
import org.swidgets.base.stooltip.SToolTip;
import org.swidgets.base.stooltip.SToolTipTypeRepository;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.MouseListener;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class SSimpleForm extends Composite  implements Validate, MouseListener{
	
	
	private SFormPanel sTitledPanel;
	private FlexTable flexTable = new FlexTable();
	private VerticalPanel vp = new VerticalPanel();
	private HorizontalPanel hp = new HorizontalPanel();
	private List fields = new ArrayList();
	private SToolTip toolTip;
	private Image icon; 
	private PopupPanel popupPanel;
	private boolean error;
	
	public SSimpleForm() {
		sTitledPanel = new SFormPanel("");
		initWidget(sTitledPanel);
		sTitledPanel.add(vp);
		vp.add(flexTable);
		vp.add(hp);
	}

	public SSimpleForm(String title) {
		
		sTitledPanel = new SFormPanel(title);
		initWidget(sTitledPanel);
		sTitledPanel.add(vp);
		vp.add(flexTable);
		vp.add(hp);
	}

	//public abstract onFormValid(Widget sender);
	//public abstract onFormInvalid(Widget sender);
	
	private boolean formValid;

	private void fieldValidate(final int index,final AsyncCallback callback) {
		if(index==fields.size()){
			if(formValid){
				errorOff();
				callback.onSuccess(new String("ok"));
			}
			else{
				errorOn("Form in errore");
				callback.onFailure(new Exception("Errore"));
			}
		}
		else{
				Validate field= (Validate)fields.get(index);
				field.validate(new AsyncCallback() {
					public void onFailure(Throwable caught) {
						formValid=false;
						fieldValidate(index+1, callback);
					}
	
					public void onSuccess(Object result) {
						fieldValidate(index+1, callback);
					}			
				});
		}
		
	}
	
	public void validate(final AsyncCallback callback) {
		if(fields.size()==0)
			callback.onSuccess(new String("ok"));
		formValid=true;
		fieldValidate(0, callback);
	}
	
	
	public void setField( int row, int column,Validate field){
		addCell(row,column);
		flexTable.setWidget(row,column,(Widget) field );
		fields.add(field);
	}
	
	public void setWidGet( int row, int column, Widget field){
		addCell(row,column);
		flexTable.setWidget(row,column,field);
	}
	
	private void addCell(int row, int column){
		if (!flexTable.isCellPresent(row, column)){
			flexTable.setText(row, column, "");
		}
	}
	
	public void errorOn(String errorMessage) {
		
		if(toolTip==null)
			toolTip=  new SToolTip();
		
		toolTip.setMessage(SToolTipTypeRepository.error, errorMessage);
		toolTip.setVisible(true);
		
		if(icon==null){
			icon = new Image("style/img/icons/error.gif");
			icon.addMouseListener(this);
			hp.add(icon);
		}
		
		sTitledPanel.addMessage(icon);
		icon.setVisible(true);
		error = true;
	}

	
	
	
	public void errorOff() {
		error = false;
		icon.setVisible(false);
		toolTip.setVisible(false);
	}

	

	public boolean isValidated() {
		return false;
	}
	
	public void addButton(Button sbutton){
		hp.add(sbutton);
	}

	public void onMouseDown(Widget sender, int x, int y) {
		// TODO Auto-generated method stub
		
	}

	public void onMouseEnter(Widget sender) {
		if(popupPanel==null)
		popupPanel = new PopupPanel();
		popupPanel.clear();
		popupPanel.setPopupPosition(icon.getAbsoluteLeft()+20, icon.getAbsoluteTop()+5);
		popupPanel.add(toolTip);
		popupPanel.show();
	}

	public void onMouseLeave(Widget sender) {
		popupPanel.hide();
		
	}

	public void onMouseMove(Widget sender, int x, int y) {
		// TODO Auto-generated method stub
		
	}

	public void onMouseUp(Widget sender, int x, int y) {
		// TODO Auto-generated method stub
		
	}
	
}
